import { defHttp } from '@/utils/http/axios';
import {
  CategoryItem,
  CategorySearch,
  CategorySearchPage,
  CreateCategory,
  UpdateCategory,
} from '#/goods-manage/category-manage/category';

enum Api {
  CategoriesOption = '/goods-server/category-manage/categories/option',
  Categories = '/goods-server/category-manage/categories',
  CategoriesCount = '/goods-server/category-manage/categories/count',
  CreateCategory = '/goods-server/category-manage/category',
  UpdateCategory = '/goods-server/category-manage/categories/id/{id}',
}

export const categories = (params?: Partial<CategorySearchPage>) => {
  return defHttp.post<CategoryItem[]>(
    { url: Api.Categories, params: params },
    { errorMessageMode: 'none' },
  );
};

export const categoriesCount = (params?: Partial<CategorySearch>) => {
  return defHttp.get<number>(
    { url: Api.CategoriesCount, params: params },
    { errorMessageMode: 'none' },
  );
};
export const createCategory = (params: CreateCategory) =>
  defHttp.post({ url: Api.CreateCategory, params: params }, { errorMessageMode: 'modal' });

export const updateCategory = (id: string, params: UpdateCategory) =>
  defHttp.post(
    { url: Api.UpdateCategory.replace('{id}', id), params: params },
    { errorMessageMode: 'modal' },
  );
